package com.szy.system.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author : zhenyun.su
 * @Commit : String字符串工具
 * 时区表示：
 * 中国： Asia/Shanghai GMT+8 GMT+8:00  GMT格式： GMT+|-Hours:Minutes
 * 美国： America/Los_Angeles
 * history
 *   2021-03-22
 *    public String utfString(String value)  将字符串转换为utf-8编码，解决乱码问题
 * @since : 2019/1/8
 */

@Deprecated
public abstract class SzyStringUtils {
    public static final String UTF_8 = "UTF-8";
    public static final String TIME_ZONE = "Asia/Shanghai";
    public static final String LOCALE = "zh-CN";

    public static final String PERCENT = "%";
    public static final String COMMA = ",";
    public static final String WINDOW_SLASH = "/";
    public static final String LINUX_SLASH = "'\'";
    public static String[] RMB_UPPER_CASE = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};

    public final static String TRUE = "true";
    public final static String FALSE = "false";
    public final static String SUCCESS = "success";
    public final static String FAILURE = "failure";
    public final static String YES = "yes";
    public final static String NO = "no";

    /**
     * @comment : 返回： 字符串， 若空，取 ""
     */
    public static String emptyString(String str) {
        return str == null ? "" : str.trim();
    }

    /**
     * @comment : 按UTF-8编码，截取length长度字符串
     * 例如：  subStringByUTF8("中国33人", 4)，返回中国
     */
    public static String subStringByUTF8(String value, Integer length) {
        try {
            if ((value == null) && (value == "")) {
                return null;
            }
            byte[] sourceBytes = value.getBytes(UTF_8);
            if (sourceBytes.length <= length) {
                return value;
            } else {
                byte[] targetBytes = new byte[length];
                System.arraycopy(sourceBytes, 0, targetBytes, 0, length - 1);
                return new String(targetBytes);
            }
        } catch (Exception E) {
            // nothing
            return null;
        }
    }

    /**
     * @comment : 返回 %str%字符串，可用于spring-data-jpa like查询
     */
    public static String percent(String str) {
        return PERCENT + emptyString(str) + PERCENT;
    }

    /**
     * @comment : 生成count位随机整数
     */
    public static int randomInt(int count) {
        return (int) ((Math.random() * 9 + 1) * 10 * count);
    }

    public static String randomInt(int min, int max) {
        int ran = (int) (Math.random() * (max - min) + 1);
        return String.valueOf(ran);
    }
    public static String Replace(String value) {
        String regEx = "[`~!@#$%^&*()\\-+={}':;,\\[\\].<>/?￥%…（）_+|【】‘；：”“’。，、？\\s]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(value);
        return m.replaceAll("");
    }
    /**
     * @comment : 根据字符串和分隔符，返回String的List对象
     * 例如：  asStringList("dog@cat@bear", "@")
     */
    public static List<String> asStringList(String values, String regex) {
        if (values == null) {
            return null;
        }
        if (regex == null) {
            regex = COMMA;
        }
        return Arrays.asList(values.split(regex));
    }

    /**
     * @comment : 根据字符串和分隔符，返回Integer的List对象
     * 例如：  asIntegerList("1,2,3", ",")
     */
    public static List<Integer> asIntegerList(String values, String regex) {
        if (values == null) {
            return null;
        }
        if (regex == null) {
            regex = COMMA;
        }
        String[] arrayString = values.split(regex);
        List<Integer> lists = new ArrayList<>();
        for (String item : arrayString) {
            lists.add(Integer.valueOf(item.trim()));
        }
        return lists;
    }

    public static List<Long> asLongList(String values, String regex) {
        if (values == null) {
            return null;
        }
        if (regex == null) {
            regex = COMMA;
        }
        String[] arrayString = values.split(regex);
        List<Long> lists = new ArrayList<>();
        for (String item : arrayString) {
            lists.add(Long.valueOf(item.trim()));
        }
        return lists;
    }

    public static String utfString(String value) {
        try{
            return java.net.URLEncoder.encode(value, UTF_8);
        }catch(Exception e){
            return value;
        }
    }
}